package com.example.controller;

import com.example.mapper.ReceiptsDetailsMapper;
import com.example.mapper.ReceiptsMapper;
import com.example.mapper.StockpileMapper;
import com.example.pojo.*;
import com.example.service.GoodsCategoryService;
import com.example.service.GoodsService;
import com.example.service.StockInService;
import com.example.utils.DateUtil;
import com.example.utils.ReMap;
import com.example.utils.ReMapUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
@Controller
public class StockInController {
    @Autowired
    private StockInService stockInService;
    @Autowired
    private GoodsCategoryService categoryService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ReceiptsMapper receiptsMapper;
    @Autowired
    private StockpileMapper stockpileMapper;
    @Autowired
    private ReceiptsDetailsMapper receiptsDetailsMapper;
    @RequiresPermissions("stock:view")
    @RequestMapping("stock")
    public String stock(Map map,@RequestParam(defaultValue = "3") Integer typeid){
        map.put("gcs",categoryService.listAll());
        map.put("typeid",typeid);
        //map.put("status",2);
        return "stockIn";
    }
    @RequiresPermissions("stock:add")
    @PostMapping("stock/addStockIn")
    public String addStockIn(String type,Integer rid,Integer[] gid,Integer[] number,String[] produced_date){
        stockInService.addStockIn(type,rid,gid,number,produced_date,"张三");
        return "stockpile";
    }
    @RequiresPermissions("stock:add")
    @PostMapping("/stock/addStock")
    @ResponseBody
    @Transactional
    public ReMap addStock(HttpServletRequest request,@RequestParam(defaultValue = "3") Integer typeid){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Map<String, String[]> parameterMap = request.getParameterMap();
        Date date = new Date();
        //装配goods表
        Goods goods = new Goods();
        goods.setCode(RandomStringUtils.random(13,false,true));
        goods.setDetails(parameterMap.get("details")[0]);
        goods.setImg(parameterMap.get("img")[0]);
        goods.setName(parameterMap.get("name")[0]);
        goods.setManufacturer(parameterMap.get("manufacturer")[0]);
        goods.setSpecification(parameterMap.get("specification")[0]);
        goods.setPrice(new BigDecimal(parameterMap.get("SellingPrice")[0]));
        goods.setGcid(Integer.valueOf(parameterMap.get("gcid")[0]));
        goodsService.addGoods(goods);

        //装配Receipts表
        Receipts receipts = new Receipts();
        receipts.setTypeid(typeid);
        receipts.setCreateTime(date);
        if(StringUtils.isEmpty(parameterMap.get("productDate")[0])){
            receipts.setUpdateTime(date);
        }else {
            date =  DateUtil.getDate(parameterMap.get("productDate")[0]);
            receipts.setUpdateTime(date);
        }
        receipts.setUpdateTime(date);
        receipts.setStatus(1);
        String rno = UUID.randomUUID().toString();
        receipts.setRno(rno);
        receipts.setOperator(user.getRealName());
        receiptsMapper.insertSelective(receipts);

        //装配ReceiptsDetails表
        ReceiptsDetails receiptsDetails = new ReceiptsDetails();

        receiptsDetails.setNumber(Integer.valueOf(parameterMap.get("StorageQuantity")[0]));
        receiptsDetails.setPrice(new BigDecimal(parameterMap.get("price")[0]));

        Goods selectGoodsByName = goodsService.selectGoodsByName(parameterMap.get("name")[0]);
        receiptsDetails.setGid(selectGoodsByName.getGid());

        Receipts selectRidByRno = receiptsMapper.selectRidByRno(rno);
        receiptsDetails.setRid(selectRidByRno.getRid());
        receiptsDetailsMapper.insertSelective(receiptsDetails);

        //装配stockpile库存表
        Stockpile stockpile = new Stockpile();
        stockpile.setGid(selectGoodsByName.getGid());
        stockpile.setNumber(Integer.valueOf(parameterMap.get("StorageQuantity")[0]));
        stockpile.setType("1");
        stockpileMapper.insertSelective(stockpile);
        return ReMapUtil.success();
    }
}
